home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: qsort help
- Date: 3 Jan 1996 14:44:34 GMT
- Organization: The Internet Access Group, Inc.
- Message-ID: <4ce4oi$hsd@news.iag.net>
- References: <4ccio7$7c0@charm.magnus.acs.ohio-state.edu> <4cdomq$jgt@dub-news-svc-5.compuserve.com>
- NNTP-Posting-Host: pm2-orl9.iag.net
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.6
-
- In article <4cdomq$jgt@dub-news-svc-5.compuserve.com>,
- stuart.hayes@baesema.co.uk says...
- >
- >xiaoyi@bmecg.bme.ohio-state.edu (Xiaoyi Wu) wrote:
- >
- >>Hi, I am having some problems with the qsort routine.
- >
- >
- >>int (*compar)(int *a, int *b)
- >Function declaration should be:
- >int compar(int *a, int *b)
-
- If this function is to be passed to qsort, it needs to be something like:
-
- int compar( const void *a, const void *b)
- {
- const int *c = a, *d = b;
- return *c - *d;
- }
-
-
- <snip>
- >
- >Alternatively to declaring function in header file could just declare
- >in the function below:
- >>int return_moved_contour(int ix, int iy)
- >>{
- >int (*compar)(int *a, int *b);
-
- This defines compar as a pointer to a function that accepts two int pointers
- and returns an int. It is uninitialized. So, it does not actually point to
- any predictable memory location (like a valid function). If you pass this to
- qsort, ... Well, lets just say that the results might not be what you want
- (Assuming that your compiler let you pass it, since it is not what qsort is
- defined to accept)
-
- >> int i, ac, count=0;
- >> int y_coord[10]; /* stores y coordinates whose x == ix */
- >
- >> for (ac=0; ac<areaindex; ac++)
- >> {
- >> for (i=0; i<index[ac]; i++)
- >> {
- >> if (xpos[ac][i] == ix) y_coord[count++] = ypos[ac][i];
- >> }
- >> if (count == 0) continue; /* not in this contour */
- >> else {
- >> qsort(y_coord, count, sizeof(int), compar);
- <snip>
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-